package com.example.service;

import com.example.entity.*;
import com.example.util.JacksonUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

@Slf4j
@Service
public class KafkaConsumerService {

    public static final String TOPIC = "supervise";

    @Autowired
    private TemperatureHumiditySensorService temperatureHumiditySensorService;
    @Autowired
    private FlameSensorService flameSensorService;
    @Autowired
    private AirQualitySensorService airQualitySensorService;
    @Autowired
    private CoSensorService coSensorService;
    @Autowired
    private LightSensorService lightSensorService;

    @KafkaListener(topics = TOPIC)
    public void listen(String message) {

        SensorData sensorData = JacksonUtil.decode(message, SensorData.class);
//        Date now = new Date();
        if (Objects.nonNull(sensorData)) {
            Dht11 dht11 = sensorData.getDht11();
            if (Objects.nonNull(dht11)) {
                TemperatureHumiditySensor temperatureHumiditySensor = new TemperatureHumiditySensor();
                temperatureHumiditySensor.setTemperature((int) (double) dht11.temperature);
                temperatureHumiditySensor.setHumidity((int) (double) dht11.humidity);
                temperatureHumiditySensor.setCreateTime(sensorData.getCreateTime());
//                temperatureHumiditySensor.setCreateTime(now);
                // todo 插入到温湿度传感器记录表
                temperatureHumiditySensorService.addTHS(temperatureHumiditySensor);
            }
            SensorValue fire = sensorData.getFlame();
            if (Objects.nonNull(fire)) {
                FlameSensor flameSensor = new FlameSensor();
                flameSensor.setFire(255 - fire.value);
                flameSensor.setCreateTime(sensorData.getCreateTime());
                flameSensorService.addFS(flameSensor);
            }
            SensorValue light = sensorData.getLight();
            if (Objects.nonNull(light)) {
                LightSensor lightSensor = new LightSensor();
                lightSensor.setLightIntensity(255 - light.value);
                lightSensor.setCreateTime(sensorData.getCreateTime());
                lightSensorService.addLS(lightSensor);
            }
            SensorValue mq4 = sensorData.getMq4(); //空气质量
            if (Objects.nonNull(mq4)) {
                AirQualitySensor airQualitySensor = new AirQualitySensor();
                airQualitySensor.setHCHO(mq4.value);
                airQualitySensor.setCreateTime(sensorData.getCreateTime());
                airQualitySensorService.addAQS(airQualitySensor);
            }
            SensorValue mq7 = sensorData.getMq7();//一氧化碳
            if (Objects.nonNull(mq7)) {
                CoSensor coSensor = new CoSensor();
                coSensor.setCo(mq7.value);
                coSensor.setCreateTime(sensorData.getCreateTime());
                coSensorService.addCoS(coSensor);
            }
        }
        log.info("Received message: " + message);
    }


    /**
     * 温湿度 3min一次
     * MQ4  3S一次 数字 状态
     * MQ7
     * 火焰
     * 光敏
     */
    @Data
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class SensorData {
        private Dht11 dht11;
        private SensorValue mq4;
        private SensorValue mq7;
        private SensorValue flame;
        private SensorValue light;
        private SensorValue infrared;
        private SensorValue sound;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private Date createTime;
    }

    @Data
    public static class Dht11 {
        private Double temperature;
        private Double humidity;
    }

    @Data
    public static class SensorValue {
        private Integer value;
        private Boolean status;
    }

}
